package com.tiny.tinyweb.core.ldap;

import java.io.IOException;

import javax.naming.NamingException;

import org.springframework.core.io.ClassPathResource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.ldap.test.LdapTestUtils;

public class LdapBatch {
    /**
     * LDAP 操作模版对象。
     */
    private LdapTemplate ldapTemplate;

    /**
     * 设置 LDAP 操作模版对象。
     * @return odmManager LDAP 操作模版对象的新值
     */
    public void setLdapTemplate(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }

    public void resetTree(String base, String ldifFile) {
        try {
            LdapTestUtils.cleanAndSetup(this.ldapTemplate.getContextSource(),
                    LdapUtils.newLdapName(base), new ClassPathResource(ldifFile));
        } catch (NamingException ne) {
            System.out.println(ne.getMessage());
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }

    public void clearTree(String base) {
        try {
            LdapTestUtils.clearSubContexts(this.ldapTemplate.getContextSource(), LdapUtils.newLdapName(base));
        } catch (NamingException ne) {
            System.out.println(ne.getMessage());
        }
    }

    public void loadLdif(String base, String ldifFile) {
        try {
            LdapTestUtils.loadLdif(this.ldapTemplate.getContextSource(),
                    new ClassPathResource(ldifFile));
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }
}
